import {jsPDF} from "jspdf";
import Store from "../store";
import {luckysheetDrawMain} from "../global/draw";

/**
 * 导出数据
 * 逻辑
 */
class ExportData {
    #a4_height = 841.89;// a4纸张高度
    #a4_width = 592.28;// a4纸张宽度
    constructor() {
    }

    ExportPDF(fileName) {
        let table_height = Store.visibledatarow[Store.visibledatarow.length - 1],// 画图区域宽度
            table_width = Store.visibledatacolumn[Store.visibledatacolumn.length - 1];// 画图区域高度
        let canvas_height = table_height,// 画布高度
            canvas_width = table_width + 120,// 画布宽度
            pageHeight = (this.#a4_width * canvas_height) / canvas_width,
            page_top = 30,// 页眉
            pdf = new jsPDF('', 'pt', 'a4');
        let dom = `
         <canvas id="luckysheetTableExportPDFArea" style="display: none;"  height="${canvas_height}" width="${canvas_width}"></canvas>
         `;
        $("#luckysheetTableContentF").parent().append(dom);
        luckysheetDrawMain(
            0,
            0,
            table_width,// 画图区域宽度
            table_height,// 画图区域高度
            60,
            0,
            0,
            0,
            "luckysheetTableExportPDFArea"
        );
        setTimeout(() => {
            let pageData = document.getElementById("luckysheetTableExportPDFArea").toDataURL('image/png', 1.0);
            pdf.addImage(pageData, 'png', 0, page_top, this.#a4_width, pageHeight);
            pdf.save(fileName + '.pdf', {returnPromise: true}).then(() => {
                $("#luckysheetTableExportPDFArea").remove();
            });
        }, 1000);
    }
}

export default ExportData;
